//
//  LHRecommendHotelDetailViewController.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-16.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendHotelDetailViewController.h"
#import "LHRecommendHotelDetailView.h"
#import "MyNavgationView.h"
#import "UMSocial.h"
#import "VLDContextSheet.h"
#import "VLDContextSheetItem.h"

@interface LHRecommendHotelDetailViewController ()<VLDContextSheetDelegate>
@property (nonatomic ,retain)VLDContextSheet *contextSheet;
@end

@implementation LHRecommendHotelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    [self createContextSheet];
    [self createRecommendHotelDetailView];
    [self createNavigation];
}

// 创建自定义导航栏
- (void)createNavigation
{
    MyNavgationView *myNaView = [[MyNavgationView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    
    [myNaView creatTitle:@""];
    
    myNaView.leftTarget = self;
    myNaView.leftAction = @selector(backToRecommendHotelController);
    [myNaView creatLeftButtonWithImage:@"btn_nav_back"];
    
    UIGestureRecognizer *tap = [[UIGestureRecognizer alloc]init];
    myNaView.rightTarget = self;
    myNaView.rightAction = @selector(press:);
    [myNaView creatRightButtonToShareOrCollect:@"53c7085374637" gestrue:tap];
    
    [self.view addSubview:myNaView];
    [tap release];
    [myNaView release];
    
}

- (void)backToRecommendHotelController
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 分享按钮响应方法
- (void)shareHotels
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55603d4f67e58e768200361e"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"meinv01.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:nil];
}

/**
 *  创建推荐旅店详情页面
 */
- (void)createRecommendHotelDetailView
{
    LHRecommendHotelDetailView *hotelDetail = [[LHRecommendHotelDetailView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    
    hotelDetail.hotelMo = self.hotelModel;
    
    [self.view addSubview:hotelDetail];
    
    [hotelDetail release];
}

#pragma mark - 导航分享收藏按钮实现方法
- (void) createContextSheet
{
    
    VLDContextSheetItem *item1 = [[VLDContextSheetItem alloc] initWithTitle: @"添加收藏"
                                                                      image: [UIImage imageNamed: @"add"]
                                                           highlightedImage: [UIImage imageNamed: @"add_highlighted"]];
    
    VLDContextSheetItem *item2 = [[VLDContextSheetItem alloc] initWithTitle: @"分享"
                                                                      image: [UIImage imageNamed: @"share"]
                                                           highlightedImage: [UIImage imageNamed: @"share_highlighted"]];
    
    self.contextSheet = [[[VLDContextSheet alloc] initWithItems: @[ item1, item2]]autorelease];
    self.contextSheet.delegate = self;
}

- (void) contextSheet: (VLDContextSheet *) contextSheet didSelectItem: (VLDContextSheetItem *) item {
    
    NSLog(@"Selected item: %@", item.title);
    if ([item.title isEqualToString:@"添加收藏"]) {
        [self creatNSFileManager];
    } else {
        
        [self shareHotels];
    }
}

- (void) press: (UIGestureRecognizer *) gestureRecognizer
{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        
        [self.contextSheet startWithGestureRecognizer: gestureRecognizer
                                               inView: self.view];
    }
}

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
                                 duration: (NSTimeInterval) duration
{
    
    [super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];
    
    [self.contextSheet end];
}

/**
 *  归档方法
 */

-(void) creatNSFileManager
{
    // NSFileManager 文件管理器
    // 获取路径  缓存路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 创建一个文件管理器(单例)
    NSFileManager *filemanager = [NSFileManager defaultManager];
    // 添加文件夹
    // 拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"LHRecommendHotel"];
    // 创建文件夹
    [filemanager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@", filePath);
    NSString *strPath = [filePath stringByAppendingPathComponent:@"LHRecommendHotel.kk"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
    
    if (arr == nil) {
        arr = [NSMutableArray array];
        [arr addObject:_hotelModel];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [a show];
        [a release];
    }else
    {
        int p = 0;
        
        for (LHRecommendHotelModel *m in arr) {
            if ([m.name isEqualToString:_hotelModel.name]) {
                p = 1;
            }
        }
        
        if (p == 0) {
            [arr addObject:_hotelModel];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            
            UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [a show];
            [a release];
        }else{
            UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [a show];
            [a release];
        }
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
